// pages/ConsumptionCoupon/index.js
const common = require('../../../public.js')
const utils = require('../../../utils/utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoadingBusy: false,
    cityId: 0,
    regionId: '',
    provinceId: '',
    discountCouponList: [
      
      // {exceedMoney:500,surplusNums:3,discountsMoney:1000,
      //   couponName:'文旅嘉年华济南通用券',
      //   effectiveTime:48},{couponName:'文旅嘉年华济南通用券',effectiveTime:48}
      ],
    adminDiscountCouponList: [
      // {exceedMoney:500,surplusNums:3,discountsMoney:1000,
      //   name:'文旅嘉年华济南通用券',
      //   effectiveTime:48},
      //   { name:'文旅嘉年华济南通用券',effectiveTime:48},
      //   { name:'文旅嘉年华济南通用券',effectiveTime:48}
      
    ],
    isCityPage: true,
    from: null,
    imageId: 0,
    active: 0,
    pageNum: 0,
    benefitListData:[
//       {
    
// address: "浙江大学山东工业技术研究院",
// adminCouponNums: 0,
// articleNums: 0,
// name: "春天里",
// region: "薛城区",
// storeImage: "http://feiyangimage.oss-cn-shanghai.aliyuncs.com/shop/1614669475605526.png",
// storeTypeName: "景区景点",
//       }
    ],
    nomore:true,
    isRealname:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  goBack() {
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; //上一个页面
    // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
    if (prevPage) {
        wx.navigateBack({
            delta: 1
        })
    } else {
        wx.redirectTo({
            url: '/pages/index/index',
        })
    }
},
  onChange(event) {
    this.setData({
      active:event.detail.name
    })
    console.log(event.detail.name)
    if(event.detail.name==2){
      this.getCityList(true)
      return
    }
 
    this.getCityCoupons()
  },
  onLoad: function (options) {
    // 处理实名认证返回没有option的问题
    console.log("4444",options)
    let capsule = wx.getMenuButtonBoundingClientRect()
    this.setData({
        height: capsule.height,
        top: capsule.top,
        storeId: options.id,
       
    })
    if (options) {
      const provinceId = options.provinceId
      if (!common.isEmptyString(provinceId)) {
        this.data.provinceId = provinceId
        this.setData({
          imageId: provinceId
        })
      } else {
        let cityId = options.cityId
        this.data.cityId = cityId
    
        const regionId = options && options.regionId ? options.regionId : ''
        this.data.from = options.from
        // if (regionId != '' && utils.containRegion(regionId)) {
          if (regionId != '' ) {
          this.data.regionId = regionId
          this.data.isCityPage = false
        }
        this.setData({
          // imageId: this.data.isCityPage ? cityId : regionId           //判断属于城市还是区
          imageId:this.data.regionId==1926 ? regionId : cityId
        })

      }

      const cityName = this.getTopicTitle()
      if (cityName) {
        wx.setNavigationBarTitle({
          title: '第六届' + cityName + '惠民消费季',
        })
      }
    }
  },
  // 去实名
  toRealName() {
    if (this.data.userInfo === null) {
      // 未登录
      common.showLoginPage()
    } else if (this.data.userInfo.isRealname != 1) {
      // 未实名
      common.toRealName()
    } else {
      // common.toRealName()
      common.showToast('您已实名')
    }
  },
  onShow: function () {
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          realname: res.data.user.isRealname == 1
        },()=>{
          console.log(that.data.realname)
        })
      },
      fail: function () {
        that.setData({
          userInfo: null
        })
      }
    })
    if(this.data.active==1){
      this.getCityList(true)
      return

    }
    this.getCityCoupons()
    
  },

  getCouponFinish() {
    this.getCityCoupons()
  },

  loadMore(){
    console.log("1211")
    if(this.data.active==2){
      console.log("1211")
        this.getCityList(false)
    return
    }
    
  },
  // 跳转对应店铺
  toStore(e){
    common.gotoStore(e.currentTarget.dataset.item)
  },
  /**
   * 获取城市列表
   */
  getCityList(refresh = false){
    var that = this
    if(!that.data.nomore)  return  //没有数据禁止请求
    this.setData({
      isLoadingBusy: true
    })
    let params=null
    const pageNum = refresh ? 1 : this.data.pageNum + 1
    if (!common.isEmptyString(this.data.provinceId)) {
      console.log("省券")
      // 省券
      isProvince = true
     params = {
        provinceId: this.data.provinceId
      }
    } else {
      // 这里区分了获取的是市券还是区券
      if (this.data.isCityPage) {
        // 市券
        console.log("市券")
      
        params = {
          pageNum,
          pageSize:10,
          pcregionId: this.data.cityId
        }
      } else {
        // 区券
        console.log("区券")
      params = {
          pageNum,
          pageSize:10,
          pcregionId: this.data.regionId
        }
      }
    }

    common.post('/cityDisCoupon/getCityDisCouponFreeStoreList', params, function (res) {
      if (res.status === 200) {
        that.data.pageNum = pageNum
        if (!refresh) {
           if (res.data.length === 0) {
            that.setData({
              nomore:false  //无数据不请求
            })
          }
            that.setData({
                benefitListData: [...that.data.benefitListData, ...res.data]
            })
        } else {
            if (res.data.length === 0) {
          
            }
            that.setData({
                benefitListData: res.data,
            })
        }
       
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoadingBusy: false
      })
    })
      
  },

  /**
   * 获取优惠券列表
   */
  getCityCoupons() {
    this.setData({
      isLoadingBusy: true
    })
    let url = null
    let params = null
    let isProvince = false
    if (!common.isEmptyString(this.data.provinceId)) {
      // 省券
      isProvince = true
      url = '/cityDisCoupon/getCityDisCouponListByProvinceId'
      params = {
        provinceId: this.data.provinceId
      }
    } else {
    
      // 这里区分了获取的是市券还是区券
      if (this.data.isCityPage) {
        // 市券
        url = '/cityDisCoupon/getCityDisCouponListByCityId'
        params = {
          cityId: this.data.cityId
        }
      } else {
        // 区券
        url = '/cityDisCoupon/getCityDisCouponListByRegionId'
        console.log(this.data.regionId)
        params = {
          regionId: this.data.regionId
        }
      }
    }

    const that = this
    common.post(url, params, function (res) {
      if (res.status === 200) {
        if (isProvince) {
          that.setData({
            discountCouponList: res.data
          })
        } else {
          that.setData({
            discountCouponList: res.data.discountCouponList,
            adminDiscountCouponList: res.data.adminDiscountCouponList
          })
        }
      } else {
        that.setData({
          discountCouponList: [],
          adminDiscountCouponList: []
        })
        common.showRequestErrMsg(res)
      }
    },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      })
  },

  toDetail(e) { // 跳转详情页
    let dcId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + dcId
    })
  },

  toPlatformCoupons(e) {
    let dcId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/cultureCoupons/platformCoupons/index?dcId=' + dcId
    })
  },

  getTopicTitle() {
    return utils.getTopicTitle(!common.isEmptyString(this.data.provinceId) ? this.data.provinceId : this.data.cityId, this.data.regionId)
  },

  getJumpParma() {
    let tmpList = []
    !common.isEmptyString(this.data.provinceId) && tmpList.push('provinceId=' + this.data.provinceId)
    !common.isEmptyString(this.data.cityId) && tmpList.push('cityId=' + this.data.cityId)
    !common.isEmptyString(this.data.regionId) && tmpList.push('regionId=' + this.data.regionId)
    return tmpList.join('&')
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    return {
      title: '第六届' + this.getTopicTitle() + '惠民消费季',
      imageUrl: 'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/share.jpg',
      success: (res) => { },
      fail: (res) => {
        // 分享失败
      }
    }
  },
  onShareTimeline() {
    const query = 'from=timeline&' + this.getJumpParma()
    return {
      title: '第六届' + this.getTopicTitle() + '惠民消费季',
      query
    }
  },


})